<?php

namespace addons\jieziarea\controller;


use addons\jiezicommon\service\Tools;

class Api extends \app\common\controller\Api
{
    protected $noNeedLogin = ['*'];

    /**
     * @ApiTitle (获取下一级)
     * @ApiMethod (POST)
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
     * @ApiReturn   ()
     */
    public function index()
    {
        $pid = input('pid', 0, 'intval');
        $list = db('jieziarea')->field('id,name')->where('pid', $pid)->select();
        $res['code'] = 1;
        $res['msg'] = '成功';
        $res['data'] = $list;
        return json($res);

    }

    /**
     * @ApiTitle (获取父子级树)
     * @ApiMethod (POST)
     * @ApiParams (name="level", type="float", require=false, sample="2", description="层级", default="2")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
     * @ApiReturn   ()
     */
    public function tree()
    {
        $level = input('level/d', 3);
        if($level < 0){
            $level = 0;
        }
        $list = db('jieziarea')->field('id, pid, name')->cache('jieziarea_list')->where('level', 'elt', $level)->select();
        if ($list) {
            $_list = Tools::getInstance()->list_to_tree($list, 'id', 'pid', 'children');
            $tmp = [];
            foreach ($_list as $item) {
                if (!empty($item['children'])) {
                    $tmp[] = $item;
                }
            }
            $list = $tmp;
            unset($tmp);
        }
        $this->result('ok', $list, 1, 'json');
    }

    /**
     * @ApiTitle (获取当前城市信息 by 经纬度)
     * @ApiMethod (POST)
     * @ApiParams (name="lat", type="float", require=true, sample="38", description="纬度")
     * @ApiParams (name="lng", type="float", require=true, sample="38", description="经度")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
     * @ApiReturn   ()
     */
    public function get_current_city()
    {
        $lat = floatval(input('lat'));
        $lng = floatval(input('lng'));
        $return_data = [
            'city_id' => 0,
            'city_name' => '',
            'district_id' => 0,
            'district_name' => '',
            'province_id' => 0,
            'province_name' => '',
        ];
        if ($lat > 0 && $lng > 0) {
            $tmp = \addons\jieziarea\service\Area::getInstance()->getAddressByQqmap($lat, $lng, 'city_name_code_index');
            if(empty($tmp['status'])){
                $return_data = $tmp;
            }else{
                $this->result('ok', $tmp, 0, 'json');
            }
        }
        $this->result('ok', $return_data, 1, 'json');
    }
}